<?php
require '../ThinkPHP/ThinkPHP.php';
$code = $_GET['code'];
$state = $_GET['state'];
//换成自己的接口信息
$appid = 'wx36c43fb386f2c5f0';
$appsecret = '24c3ebdb7c29553a83ede4f45715e49c';
// if (empty($code)) $this->error('授权失败');
 if (empty($code)) echo '授权失败';
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
    echo '<h1>错误：</h1>'.$token->errcode;
    echo '<br/><h2>错误信息：</h2>'.$token->errmsg;
    exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
    echo '<h1>错误：</h1>'.$access_token->errcode;
    echo '<br/><h2>错误信息：</h2>'.$access_token->errmsg;
    exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//转成对象
$user_info = json_decode(file_get_contents($user_info_url));
if (isset($user_info->errcode)) {
    echo '<h1>错误：</h1>'.$user_info->errcode;
    echo '<br/><h2>错误信息：</h2>'.$user_info->errmsg;
    exit;
}
$array = get_object_vars($user_info);
$openid = $array['openid'];
$nickname = $array['nickname'];
$headimgurl = $array['headimgurl'];

header('content-type:text/html;charset=utf-8'); 
//打印用户信息
// echo '<pre>';
// print_r($user_info);
// echo '</pre>';
$servername="my0791614.xincache1.cn";  
$username="my0791614";  
$userpassword="xh7678119";  
$dbname = "my0791614";  
$connent=new mysqli($servername,$username,$userpassword,$dbname);
// if($connent->connect_error){  
//     die("连接失败: " . $connent->connect_error);  
// }else{  
//     echo "连接成功";  
// } 
$yue=500;
$time=time();
$date=date("Y-m-d H:i:d",$time);
$ii="SELECT * FROM jy_user WHERE openid='$openid'";
$aa = mysql_fetch_row($ii);
if($aa){
     //$insertdata="INSERT INTO jy_user(yonghuname,openid,headimgurl,addtime,yue) VALUES('$nickname','$openid','$headimgurl','$date','$yue')";  
    if(mysqli_query($connent, $insertdata)){  
        $_SESSION['openid'] = $openid;
        $_SESSION['yonghuname']=$nickname;
        $_SESSION['headimgurl']=$headimgurl;
		
        echo '<script type="text/javascript">
                window.location.href="http://niceweekend.cc/jieyue/Home/My/my";
              </script>';
    }else{  
         echo "Error insert data: " . $connent->error;  
    }  
} else {
   
    if(mysqli_query($connent, $ii)){          
        $_SESSION['openid'] = $openid;
        $_SESSION['yonghuname']=$nickname;
        $_SESSION['headimgurl']=$headimgurl;
        echo '<script type="text/javascript">
                window.location.href="http://niceweekend.cc/jieyue/Home/My/my";
              </script>';
    }else{  
             echo "Error insert data: " . $connent->error;  
    }  
}

?>